package com.jc.service.zflservice;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.ExceptionUtil;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.MD5;
import com.jc.common.util.MapKeyComparatorUtil;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.framework.enums.ZFLApi;
import com.jc.framework.util.PropertyPlaceholder;
import com.jc.framework.util.SpringUtils;
import com.jc.service.systemloglist.SystemLogListService;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("all")
@Service
public class ZflCommonUtil {

    private static SystemLogListService systemLogListService = SpringUtils.getBean(SystemLogListService.class);


    public static String ZFLHttpUtil(String method,String version ,Map<String,Object> param) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatDate = simpleDateFormat.format(new Date());
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("serviceName",method);
        paramMap.put("version",version);
        paramMap.put("bizContent", JSONObject.toJSONString(param));
        paramMap.put("format", "json");
        paramMap.put("charset", "UTF8");
        paramMap.put("timestamp", formatDate);//请求时间
        paramMap.put("channelCode", PropertyPlaceholder.getProperty("zfl.channel.code"));//渠道Code
        paramMap.put("sign",sign(paramMap));
        paramMap.put("signType", "MD5");//固定MD5
        String result = HttpUtil.post(String.valueOf(PropertyPlaceholder.getProperty("zfl.base.url")), paramMap);
        return result;
    }


    /**
     * 加密请求参数字符串
     * */
    public static String sign(Map<String,Object> paramMap) throws UnsupportedEncodingException {
        paramMap = MapKeyComparatorUtil.sortMapByKey(paramMap);
        String paramString = "";
        for (String key:paramMap.keySet()) {
            Object value = paramMap.get(key);
            paramString = paramString + URLEncoder.encode(key, "UTF-8") + "="+ URLEncoder.encode(value.toString(),"UTF-8")+"&";
        }
        System.out.println("========加密之前的字符串: "+ paramString);
        String paramSub = paramString.substring(0, paramString.length()-1) + PropertyPlaceholder.getProperty("zfl.appkey");
        String paramMd5 = MD5.passmd5(paramSub);
        System.out.println("========加密之后的字符串: "+paramMd5);
        return paramMd5;
    }


    public static Map<String,Object> getProDetail(Long proId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("itemId",proId);
        String resultStr = null;
        try {
            resultStr = ZflCommonUtil.ZFLHttpUtil(ZFLApi.PRODUCT_DETAIL_METHOD_VERSION.getName(), ZFLApi.PRODUCT_DETAIL_METHOD_VERSION.getDesc(), paramMap);
            systemLogListService.addSystemLog(JSONObject.toJSONString(paramMap), Conclusion.OK, null, LogType.ZFL_GET_PRODUCT_DETAIL, resultStr);
        } catch (Exception e) {
            systemLogListService.addSystemLog(JSONObject.toJSONString(paramMap), Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 150), LogType.ZFL_GET_PRODUCT_DETAIL, resultStr);
            e.printStackTrace();
        }
        Map<String,Object> proDetail = JSONObject.parseObject(JSONObject.parseObject(resultStr, Map.class).get("bizContent").toString(), Map.class);
        return proDetail;
    }


//    static String a= "bizContent=%7B%22order%22%3A%7B%22buyerNotes%22%3A%22111222222222222%22%2C%22items%22%3A%5B%5D%2C%22outOrderId%22%3A%22941%22%2C%22shipAddress%22%3A%7B%22cityId%22%3A0%2C%22cityName%22%3A%22%E5%AE%81%E6%B3%A2%22%2C%22countyId%22%3A0%2C%22districtId%22%3A0%2C%22districtName%22%3A%22%E6%B5%B7%E6%9B%99%E5%8C%BA%22%2C%22name%22%3A%22%E5%BC%A0%E4%BA%91%E9%A3%9E%22%2C%22phone%22%3A%2213598008451%22%2C%22provinceId%22%3A0%2C%22provinceName%22%3A%22%E6%B5%99%E6%B1%9F%22%2C%22street%22%3A%22%E7%AB%8B%E5%9B%AD%E5%8C%97%E8%B7%AF755%E7%94%B5%E5%95%86%E5%A4%A7%E5%8E%A6403%22%7D%7D%7D&channelCode=jicheng&charset=UTF8&format=json&serviceName=trade.order.create.zy.channel&timestamp=2018-12-19+17%3A12%3A51&version=1.0&";
//    public static void main(String[]args) throws UnsupportedEncodingException {
//        String[] split = a.split("&");
//        for (String s:split){
//            String[] split1 = s.split("=");
//            String key = split1[0];
//            String val = split1[1];
//            System.out.println("=====key:"+ URLDecoder.decode(key,"UTF-8")+",value="+URLDecoder.decode(val,"UTF-8"));
//        }
//    }


}
